<template>
  <div class="addform">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="姓名" prop="sayName">
        <el-input v-model="ruleForm.sayName"></el-input>
      </el-form-item>
      <el-form-item label="英文名" prop="sayEname">
        <el-input v-model="ruleForm.sayEname"></el-input>
      </el-form-item>
      <!-- 图片 -->
      <el-form-item label="上传图片1:" prop="sayImg">
        <el-upload
          :action="this.uploadIndex1"
          list-type="picture-card"
          ref="upload"
          :auto-upload="false"
          :on-change="beforeAvatarUpload"
          :on-success="handleAvatarSuccess"
        >
          <i slot="default" class="el-icon-plus"></i>
          <div class="el-upload__tip" slot="tip">
            只能上传jpg/png文件，且不超过20mb
          </div>
          <!-- 图片 -->
          <div slot="file" slot-scope="{ file }">
            <img
              class="el-upload-list__item-thumbnail"
              :src="file.url"
              alt=""
            />
            <span class="el-upload-list__item-actions">
              <span class="el-upload-list__item-preview" @click="Pview(file)">
                <i class="el-icon-zoom-in"></i>
              </span>
            </span>
          </div>
        </el-upload>
      </el-form-item>
      <el-form-item label="名言内容:" prop="sayContent">
        <el-input
          type="textarea"
          v-model="ruleForm.sayContent"
          :rows="4"
          class="inputL"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">
          增加名言
        </el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 图片预览 -->
    <el-dialog :visible.sync="dialogVisible" append-to-body>
      <img width="30%" :src="dialogImageUrl" alt="" />
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogImageUrl: "",
      dialogVisible: false,
      ruleForm: {//必须保证前后端pojo类名字一样
        sayName: "",
        sayEname: "",
        sayImg: "",
        sayContent: "",
      },
      rules: {//如果没输入的提示信息
        sayName: [
          { required: true, message: "请输入作者姓名", trigger: "blur" },
          { max: 25, message: "最大长度25个字", trigger: "blur" },
        ],
        sayEname: [
          { required: true, message: "请输入作者英文名", trigger: "blur" },
          { max: 25, message: "最大长度25个字", trigger: "blur" },
        ],
        sayContent: [
          { required: true, message: "请输入作者名言", trigger: "blur" },
          { max: 100, message: "最大长度100个字", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    // 预览 无需管
    Pview(file) {
      console.log(file.url);
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    // 上传图片判断
    beforeAvatarUpload(file) {
      const isLt10M = file.size / 1024 / 1024 < 10;
      console.log("文件大小:" + (file.size / 1024 / 1024).toFixed(2) + "MB");
      if (!isLt10M) {
        this.$message.error("上传图片大小不能超过 10MB!");
        this.$refs.upload.clearFiles();
      } else {
        console.log(file)
        this.$refs.upload.submit();
      }
      return isLt10M;
    },
    //上传
    handleAvatarSuccess(res, file) {
      this.ruleForm.sayImg = "/assets/myIndex/img/" + file.name;
      console.log("图片:" + this.ruleForm.sayImg);
    },
    submitForm(formName) {
      // 提交方法 先判断提交表单是否有空=>validate
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log("进入增加");
          // 由于axios内部必须使用_this才能使用this
          const _this = this;
          this.axios({
            method: "post",
            url: _this.baseUrl + "/index/post/addSay",
            //传递给后端的数据
            data: _this.ruleForm
          }).then(function (resp) {
            console.log(resp.data);
            _this.$parent.$parent.successAdd();
          });
        } else {
          console.log("增加失败");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.addform {
  width: 80%;
  margin: 0 auto;
  .inputS {
    width: 80%;
  }
  .inputL {
    width: 80%;
    display: block;
  }
  .imgPreview {
    width: 150px;
    height: 150px;
    margin-right: 15px;
    float: left;
    cursor: pointer;
  }
}
</style>